""" 获取摄像头 截图"""

import requests
from requests.auth import HTTPDigestAuth
import os  

import csv

def 截取摄像头图片(ip,user,passwd,fname,savePath='.'):
    urlTmp= "http://{}/ISAPI/Streaming/channels/101/picture".format(ip)
    url = urlTmp
    try: #
        resp = requests.get(url,auth=HTTPDigestAuth(user, passwd), timeout=(5,5))
        if resp.status_code != 200 :
            return 
        img = resp.content
        #保存路径
        path=os.path.join(savePath,'{}.jpg'.format(fname))
        with open(path, 'wb') as f:
            f.write(img)
    except Exception as ex:
        print("--访问{}出错----".format(url),ex)


def 读取csv列表(fcsv):
    with open(fcsv) as csvfile:
        reader = csv.DictReader(csvfile)
        column = [row  for row in reader]   # weight 同列的数据
    return  column

if __name__ == '__main__':

    fcsv = r"132.csv"
    rows = 读取csv列表(fcsv)
    savePath = '程序截图目录'

    if os.path.exists(savePath) == False:
        os.mkdir(savePath)


    errLs = [] 
    for i in rows:
        try:
            截取摄像头图片(i['IP地址'],'admin' ,'wsfa2019',i['IP地址']+'@'+i['物理地址'],savePath)
            print(i['IP地址'] )
        except Exception as e:
            errLs.append(i)
            print(i['IP地址'],i['物理地址'])
    

    # ip = '192.168.6.6'
    # 截取摄像头图片(ip,'摄像头图片',savePath)
        
